package com.mls.fivedistrict.controller.backend;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Dynamic;
import com.mls.fivedistrict.server.DynamicService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 动态 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-07-06
 */
@RestController("backendDynamicController")
@RequestMapping("/api/manager")
public class DynamicController extends BaseController {
    @Autowired
    DynamicService dynamicService;

    @PostMapping("/v1/dynamic")
    public ServiceResponse addDynamic(@RequestBody Dynamic dynamic) {
        BeanValidator.check(dynamic);
        boolean insert = dynamicService.insert(dynamic);
        if (insert) {
            return ServiceResponse.createSuccessResponse("添加动态成功");
        }
        return ServiceResponse.createErrorResponse("添加动态失败");
    }

    @DeleteMapping("/v1/dynamic/{dynamicId}")
    public ServiceResponse deleteDynamic(@PathVariable Long dynamicId) {
        Optional.ofNullable(dynamicId).orElseThrow(() -> new CustomerException("动态id不能为空"));
        Dynamic dynamic = new Dynamic();
        dynamic.setId(dynamicId);
        dynamic.setDelete(true);
        boolean delete = dynamicService.updateById(dynamic);
        if (delete) {
            return ServiceResponse.createSuccessResponse("删除动态成功");
        }
        return ServiceResponse.createErrorResponse("删除动态失败");
    }

    @PutMapping("/v1/dynamic/{dynamicId}")
    public ServiceResponse updateDynamic(@PathVariable Long dynamicId, @RequestBody Dynamic dynamic) {
        Optional.ofNullable(dynamicId).orElseThrow(() -> new CustomerException("动态id不能为空"));
        Optional.ofNullable(dynamic).orElseThrow(() -> new CustomerException("动态内容不能为空"));
        dynamic.setId(dynamicId);
        boolean update = dynamicService.updateById(dynamic);
        if (update) {
            return ServiceResponse.createSuccessResponse("修改动态成功");
        }
        return ServiceResponse.createErrorResponse("修改动态失败");
    }

    @GetMapping("/v1/dynamic/{dynamicId}")
    public ServiceResponse getDynamic(@PathVariable Long dynamicId) {
        Optional.ofNullable(dynamicId).orElseThrow(() -> new CustomerException("动态id不能为空"));
        Dynamic dynamic = dynamicService.selectById(dynamicId);
        if (null != dynamic) {
            return ServiceResponse.createSuccessResponse(dynamic);
        }
        return ServiceResponse.createErrorResponse("查无该动态");
    }

    @GetMapping("/v1/dynamics")
    public ServiceResponse getDynamicList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List selectList = dynamicService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo<Dynamic>(selectList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }
}

